/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.unitn.disi.wikireputation.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author carlos
 */
public class DBConnection {
    private static String user = "root";
    private static String pass = "root";
    private static String url  = "jdbc:mysql://localhost:8889/simple";
    private static Connection conn = null;
    
    public static Connection getConnection() throws ClassNotFoundException, SQLException{

        if(conn == null){
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, user, pass);
        }

        return conn;
        
    }

    public static Connection getConnection(String connUrl, String username, String password) throws ClassNotFoundException, SQLException{
        user = username;
        pass = password;
        url  = connUrl;

        return getConnection();
    }

    public static void closeConnection(){

        try{
            if(conn != null && !conn.isClosed())
                conn.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        
    }

    public static void setConnection(Connection connection){
        conn = connection;
    }

}
